#include "startup_md.h"
#include "stm32f10x.h"
#include "EnvSetting.h"
#include "BasicInterface.h"
#include <cstdlib>
#include <exception>
extern DEBUG_CLASS DEBUG_OBJECT;
std::uint8_t Stack_Mem[Stack_Size] __attribute__((section("STACK")));
std::uint8_t Heap_Mem[Heap_Size] __attribute__((section("HEAP")));
const char* args[] = {__DATE__, __FILE__, __TIME__};
const IsrVector_t __Vectors[] =
{
	/***********************************************************/
	__initial_sp,
	Reset_Handler,
	NMI_Handler,
	HardFault_Handler,
	MemManage_Handler,
	BusFault_Handler,
	UsageFault_Handler,
	0, 0, 0, 0,
	SVC_Handler,
	DebugMon_Handler,
	0,
	PendSV_Handler,
	SysTick_Handler,
	/************************************************************/
	WWDG_IRQHandler,
	PVD_IRQHandler,
	TAMPER_IRQHandler,
	RTC_IRQHandler,
	FLASH_IRQHandler,
	RCC_IRQHandler,
	EXTI0_IRQHandler,
	EXTI1_IRQHandler,
	EXTI2_IRQHandler,
	EXTI3_IRQHandler,
	EXTI4_IRQHandler,
	DMA1_Channel1_IRQHandler,
	DMA1_Channel2_IRQHandler,
	DMA1_Channel3_IRQHandler,
	DMA1_Channel4_IRQHandler,
	DMA1_Channel5_IRQHandler,
	DMA1_Channel6_IRQHandler,
	DMA1_Channel7_IRQHandler,
	ADC1_2_IRQHandler,
	USB_HP_CAN1_TX_IRQHandler,
	USB_LP_CAN1_RX0_IRQHandler,
	CAN1_RX1_IRQHandler,
	CAN1_SCE_IRQHandler,
	EXTI9_5_IRQHandler,
	TIM1_BRK_IRQHandler,
	TIM1_UP_IRQHandler,
	TIM1_TRG_COM_IRQHandler,
	TIM1_CC_IRQHandler,
	TIM2_IRQHandler,
	TIM3_IRQHandler,
	TIM4_IRQHandler,
	I2C1_EV_IRQHandler,
	I2C1_ER_IRQHandler,
	I2C2_EV_IRQHandler,
	I2C2_ER_IRQHandler,
	SPI1_IRQHandler,
	SPI2_IRQHandler,
	USART1_IRQHandler,
	USART2_IRQHandler,
	USART3_IRQHandler,
	EXTI15_10_IRQHandler,
	RTCAlarm_IRQHandler,
	USBWakeUp_IRQHandler
};
__weak CODE_SECTION void Reset_Handler(void)
{
	SystemInit();
	__main();
}
__weak CODE_SECTION void NMI_Handler(void)
{
}
__weak CODE_SECTION void HardFault_Handler(void)
{
	DEBUG_CLASS &_dbg = DEBUG_OBJECT;
	_dbg.Printf("HardFault_Handler");
	while(true);
}
__weak CODE_SECTION void MemManage_Handler(void)
{
	while(true);
}
__weak CODE_SECTION void BusFault_Handler(void)
{
	while(true);
}
__weak CODE_SECTION void UsageFault_Handler(void)
{
	while(true);
}
__weak CODE_SECTION void SVC_Handler(void)
{
}
__weak CODE_SECTION void DebugMon_Handler(void)
{
}
__weak CODE_SECTION void PendSV_Handler(void)
{
}
__weak CODE_SECTION void SysTick_Handler(void)
{
}

__weak CODE_SECTION void WWDG_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void PVD_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void TAMPER_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void RTC_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void FLASH_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void RCC_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void EXTI0_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void EXTI1_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void EXTI2_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void EXTI3_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void EXTI4_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void DMA1_Channel1_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void DMA1_Channel2_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void DMA1_Channel3_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void DMA1_Channel4_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void DMA1_Channel5_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void DMA1_Channel6_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void DMA1_Channel7_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void ADC1_2_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void USB_HP_CAN1_TX_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void USB_LP_CAN1_RX0_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void CAN1_RX1_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void CAN1_SCE_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void EXTI9_5_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void TIM1_BRK_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void TIM1_UP_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void TIM1_TRG_COM_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void TIM1_CC_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void TIM2_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void TIM3_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void TIM4_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void I2C1_EV_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void I2C1_ER_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void I2C2_EV_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void I2C2_ER_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void SPI1_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void SPI2_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void USART1_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void USART2_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void USART3_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void EXTI15_10_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void RTCAlarm_IRQHandler(void)
{
	while(true);
}
__weak CODE_SECTION void USBWakeUp_IRQHandler(void)
{
	while(true);
}
#if defined(__MICROLIB)

#else
void __user_initial_stackheap(void)
{
	register std::uint32_t r0 __ASM("r0");
	register std::uint32_t r1 __ASM("r1");
	register std::uint32_t r2 __ASM("r2");
	register std::uint32_t r3 __ASM("r3");
	r0 = (std::uint32_t)Heap_Mem;
	r1 = ((std::uint32_t)Stack_Mem + Stack_Size);
	r2 = ((std::uint32_t)Heap_Mem + Heap_Size);
	r3 = (std::uint32_t)Stack_Mem;
	return;
}
#endif
#pragma O0
extern "C"
{
extern int $Super$$main(const int, const char**);
void $Sub$$main()
{
#if RUN_TIP == 1
	int ret;
	BasicDebug &_dbg = DEBUG_OBJECT;
	try
	{
		ret = $Super$$main(sizeof(args) / sizeof(*args), args);//跳转至main
		_dbg.Printf("Program exited with return value %d\r\n", ret);
	}
	catch(const std::exception& e)
	{
		_dbg.Printf("Uncatched Exception Object:%s\r\n", e.what());
	}
	catch(const int e)
	{
		_dbg.Printf("Uncatched Int Exception:%d\r\n", e);
	}
	catch(const char* e)
	{
		_dbg.Printf("Uncatched String Exception:%s\r\n", e);
	}
	std::exit(ret);
#elif RUN_TIP == 2
	int ret;
	BasicDebug &_dbg = DEBUG_OBJECT;
	ret = $Super$$main(sizeof(args) / sizeof(*args), args);//跳转至main
	_dbg.Printf("Program exited with return value %d\r\n", ret);
	std::exit(ret);
#elif RUN_TIP == 3
	std::exit($Super$$main(sizeof(args) / sizeof(*args), args));
#else
#error "RUN_TIP Set Error"
#endif
}
}
